<template>
	<view class="upload">
		<view class="content">
			<view class="introduce">
				<textarea class="introduce_input" maxlength="12" v-model="upload_text" placeholder="添加作品描述.." />
				<view class="video_content">
					<video class="video" :src="video" :show-center-play-btn="false" :controls="false" :autoplay="false">
					</video>
					<view class="choose" @click="chooseVideo">重新选择</view>
				</view>
			</view>
			<view class="setup">
				<view class="visible-box">
					<view class="visible-left">
						<view class="left-icon iconfont icon-quxiaosuodingjiesuo"></view>
						<view class="left-text">公开·所有人可见</view>
					</view>
					<view class="visible-right iconfont icon-qianjin"></view>
				</view>
				<view class="visible-box">
					<view class="visible-left">
						<view class="left-icon iconfont icon-shezhi"></view>
						<view class="left-text">高级设置</view>
					</view>
					<view class="visible-right iconfont icon-qianjin"></view>
				</view>
			</view>
		</view>
		<view class="upload-btn" @click="upload" :style="upload_text==''||video==''?'background-color:#b41f3d;':''">
			<view class="upload-icon iconfont icon-shangchuan"></view>
			<view class="upload-text">上传</view>
		</view>
	</view>
</template>

<script>
	import t from '../../common/helper.js';
	
	export default {
		data() {
			return {
				ip: t.o,
				video: '',
				upload_text: '',
			}
		},
		methods: {
			//选择视频
			chooseVideo() {
				const that = this;
				uni.chooseVideo({
					sourceType: ['camera', 'album'],
					success: res => {
						that.video = res.tempFilePath;
					}
				})
			},
			//上传视频
			upload() {
				uni.showLoading();
				if(this.upload_text != '' && this.video!='') {
					const that = this;
					const userId = uni.getStorageSync('user')[0].userId;
					uni.request({
						url: t.o+'/myvideo',
						method: 'POST',
						data: {
							userId: userId,
						},
						success: res => {
							const name = res.data.data.length == 0 ? 
													 userId + '-' +'1.mp4' : userId + '-' + (res.data.data[res.data.data.length-1].id+1) + '.mp4';
							uni.uploadFile({
								url: t.o+'/video', //仅为示例，非真实的接口地址
								filePath: that.video,
								name: 'file',
								formData: {
								    'userId': userId,
										'name': name, 
										'type': 'video',
										'text': that.upload_text,
								},
								success: res => {
									uni.hideLoading();
									uni.showToast({
										title: '上传成功',
									});
									setTimeout(() => {
										getApp().$upload = true;
										uni.switchTab({
											url: '/pages/personal/personal',
										})
									},1000)
								}
							})
						}
					})
				}else {
					uni.hideLoading()
				}
			}
		},
		mounted() {
			this.chooseVideo();
		}
	}
</script>

<style lang="scss">
	.upload {
		height: 100%;
		color: #FFFFFF;
		position: relative;
		// &::before {
		// 	content: '';
		// 	width: 100%;
		// 	height: 1px;
		// 	background-color: #808080;
		// 	overflow: hidden;
		// 	position: absolute;
		// 	top: 0;
		// 	left: 0;
		// 	transform: translateY(-90%);
		// }
		.content {
			box-sizing: border-box;
			padding: 20px 20px;
			.introduce {
				color: #999999;
				height: 100px;
				display: flex;
				justify-content: space-between;
				border-bottom: 1rpx;
				position: relative;
				padding-bottom: 20px;
				&::before {
					content: '';
					width: 100%;
					height: 1px;
					background-color: #808080;
					overflow: hidden;
					position: absolute;
					bottom: 0;
					left: 0;
					transform: translateY(-90%);
				}
				.introduce_input {
					flex: 0 0 60%;
					height: 70px;
					color: #FFFFFF;
				}
				.video_content {
					flex: 0 0 30%;
					position: relative;
					.video {
						width: 100%;
						height: 100%;
					}
					.choose {
						position: absolute;
						bottom: 0;
						width: 100%;
						color: #FFFFFF;
						height: 30px;
						font-size: 14px;
						display: flex;
						align-items: center;
						justify-content: center;
						background-color: rgba(0,0,0,.7);
					}
				}
			}
			.setup {
				margin-top: 10px;
				.visible-box {
					padding: 15px 0;
					width: 100%;
					height: 30px;
					display: flex;
					align-items: center;
					justify-content: space-between;
					.visible-left {
						flex: 60%;
						height: 30px;
						display: flex;
						align-items: center;
						.left-icon {
							font-size: 25px;
						}
						.left-text {
							margin-left: 20px;
							font-size: 16px;
						}
					}
					.visible-right {
						flex: 0 0 20px;
						font-size: 14px;
						color: #999999;
						// background-color: #fe2c55;
					}
				}
			}
		}
		.upload-btn {
			position: absolute;
			bottom: 20px;
			left: 50%;
			transform: translateX(-50%);
			width: 80%;
			height: 50px;
			background-color: #fe2c55;
			border-radius: 2px;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 16px;
			.upload-icon {
				font-size: 34px;
			}
		}
	}
</style>
